package com.iknight.mgr.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.mgr.api.UserService;
import com.iknight.mgr.mapper.RoleMapper;
import com.iknight.mgr.mapper.UserMapper;
import com.iknight.mgr.model.RoleModel;
import com.iknight.mgr.model.UserModel;

@Service
public class UserServiceImpl implements UserService<UserModel, String> {

	@Autowired
	private UserMapper<UserModel, String> userMapper;
	@Autowired
	private RoleMapper<RoleModel, String> roleMapper;

	public void save(UserModel record) {
		// TODO Auto-generated method stub
		if (userMapper.selectOne(record.getUser_id()) != null) {
			userMapper.update(record);
		} else {
			userMapper.insert(record);
		}
	}

	@Transactional
	public void delete(String pKey) {
		// TODO Auto-generated method stub
		roleMapper.releaseUserFromRole(pKey);
		userMapper.delete(pKey);
	}

	public UserModel select(String pKey) {
		// TODO Auto-generated method stub
		return userMapper.selectOne(pKey);
	}

	public void updateUserPwd(String pKey, String newPwd, String saltValue) {
		// TODO Auto-generated method stub
		userMapper.updateUserPwd(pKey, newPwd, saltValue);
	}

	public UserModel selectOne(String pkey) {
		// TODO Auto-generated method stub
		return userMapper.selectOne(pkey);
	}

	public PageInfo<UserModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageStart, pageSize, true);
		List<UserModel> result = userMapper.selectByCond(params);
		PageInfo<UserModel> page = new PageInfo<UserModel>(result);
		return page;
	}

	public UserModel getUserByUsername(String username) {
		// TODO Auto-generated method stub
		UserModel record = userMapper.getUserInfoByName(username);
//		RoleModel record = roleMapper.selectOne("f4733da1-06c5-11e7-8eb0-54ee7542683a");
		return record;
	}

}
